package com.ruoyi.qsng.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.AjaxResults;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.SnowFlakeUtil;
import com.ruoyi.qsng.domain.*;
import com.ruoyi.qsng.mapper.KCourseMapper;
import com.ruoyi.qsng.mapper.KCoursePurchaseMapper;
import com.ruoyi.qsng.mapper.TUserPurchaseMapper;
import com.ruoyi.qsng.service.IKCoursePurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qsng.mapper.KCoursePurchaseMapper;
import com.ruoyi.qsng.domain.KCoursePurchase;
import com.ruoyi.qsng.service.IKCoursePurchaseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 课程购买Service业务层处理
 *
 * @author kanglou
 * @date 2023-06-07
 */
@Service
public class KCoursePurchaseServiceImpl implements IKCoursePurchaseService {
    @Autowired
    private KCoursePurchaseMapper kCoursePurchaseMapper;

    @Autowired
    private KCourseMapper kCourseMapper;

    @Autowired
    private TUserPurchaseMapper tUserPurchaseMapper;

    /**
     * 查询课程购买
     * 
     * @param id 课程购买主键
     * @return 课程购买
     */
    @Override
    public KCoursePurchase selectKCoursePurchaseById(String id) {
        return kCoursePurchaseMapper.selectKCoursePurchaseById(id);
    }

    /**
     * 查询课程购买列表
     *
     * @param kCoursePurchase 课程购买
     * @return 课程购买
     */
    @Override
    public List<KCoursePurchase> selectKCoursePurchaseList(KCoursePurchase kCoursePurchase) {
        return kCoursePurchaseMapper.selectKCoursePurchaseList(kCoursePurchase);
    }

    /**
     * 查询课程购买列表
     *
     * @param kCoursePurchase 课程购买
     * @return 课程购买集合
     */
    @Override
    public List<KCoursePurchase> selectKCoursePurchaseDataList(KCoursePurchase kCoursePurchase) {
        return kCoursePurchaseMapper.selectKCoursePurchaseDataList(kCoursePurchase);
    }

    /**
     * 查询课程购买列表
     *
     * @param kCoursePurchase 课程购买
     * @return 课程购买
     */
    @Override
    public List<KCoursePurchase> selectKCoursePurchaseListForApp(KCoursePurchase kCoursePurchase)
    {
        return kCoursePurchaseMapper.selectKCoursePurchaseListForApp(kCoursePurchase);
    }

    /**
     * 新增课程购买
     * 
     * @param kCoursePurchase 课程购买
     * @return 结果
     */
    @Override
    public int insertKCoursePurchase(KCoursePurchase kCoursePurchase)
    {

        kCoursePurchase.setId(SnowFlakeUtil.getNextId());
        kCoursePurchase.setCreateTime(DateUtils.getNowDate());
        return kCoursePurchaseMapper.insertKCoursePurchase(kCoursePurchase);
    }


    /**
     * 新增课程购买
     *
     * @param kCoursePurchase 课程购买
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResults insertKCoursePurchaseForApp(KCoursePurchase kCoursePurchase)
    {
        //先判断人数是否已满
        KCourse kCourse = kCourseMapper.selectKCourseById(kCoursePurchase.getCourseId());
        Assert.notNull(kCourse, "未查到该课程！");


        if((kCourse.getTotalQuota() - kCourse.getSold())<=0){
            return AjaxResults.error("报名人数已满！");
        }

        //判断时间
        if(! isEffectiveDate(new Date(),kCourse.getBmKssj(),kCourse.getBmJssj())){
            return AjaxResults.error("该课程不在报名时间内！");
        }

//        //更新人数
//        kCourse.setRemainingQuota2(kCourse.getRemainingQuota2());
//        int i=kCourseMapper.updateKCourse(kCourse);
//        if(i==0){
//            return AjaxResults.error("更新报名人数失败！");
//        }

        String id=SnowFlakeUtil.getNextId();
        kCoursePurchase.setId(id);
        kCoursePurchase.setCreateTime(DateUtils.getNowDate());
        kCoursePurchaseMapper.insertKCoursePurchase(kCoursePurchase);
        return AjaxResults.success(id);
    }

    /**
     * 新增课程购买
     *
     * @param kCoursePurchase 课程购买
     * @return 结果
     */
    @Transactional
    @Override
    public AjaxResults insertKCoursePurchaseForApp(KCoursePurchase kCoursePurchase, KUsers kUsers)
    {
        //先判断人数是否已满
        KCourse kCourse = kCourseMapper.selectKCourseById(kCoursePurchase.getCourseId());
        Assert.notNull(kCourse, "未查到该课程！");


        if((kCourse.getTotalQuota() - kCourse.getSold())<=0){
            return AjaxResults.error("报名人数已满！");
        }

        //判断时间
        if(! isEffectiveDate(new Date(),kCourse.getBmKssj(),kCourse.getBmJssj())){
            return AjaxResults.error("该课程不在报名时间内！");
        }

//        //更新人数
//        kCourse.setRemainingQuota2(kCourse.getRemainingQuota2());
//        int i=kCourseMapper.updateKCourse(kCourse);
//        if(i==0){
//            return AjaxResults.error("更新报名人数失败！");
//        }

        String id=SnowFlakeUtil.getNextId();
        kCoursePurchase.setId(id);
        kCoursePurchase.setCreateTime(DateUtils.getNowDate());
        kCoursePurchaseMapper.insertKCoursePurchase(kCoursePurchase);

        //生成购买信息，
        TUserPurchase tUserPurchase =new TUserPurchase();
        tUserPurchase.setName(kUsers.getNickName());
        tUserPurchase.setAge(Long.valueOf(kUsers.getNickAge()));
        tUserPurchase.setNation(kUsers.getNickNation());
        tUserPurchase.setIdCard(kUsers.getNickCard());
        tUserPurchase.setParentName(kUsers.getHomeName());
        tUserPurchase.setParentPhone(kUsers.getHomePhone());
        tUserPurchase.setSchool(kUsers.getNickSchool());
        tUserPurchase.setGrade(kUsers.getNickLevel());
        tUserPurchase.setIsOwn(kUsers.getIsOwn());
        tUserPurchase.setOrderId(id);
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        Assert.notNull(userId, "获取用户信息失败！");
        tUserPurchase.setCreateBy(String.valueOf(userId));
        tUserPurchase.setCreateTime(new Date());
        int i=tUserPurchaseMapper.insertTUserPurchase(tUserPurchase);
        if(i==0){
            return AjaxResults.error("生成订单失败！");
        }
        return AjaxResults.success(id);
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意三个参数的时间格式要一致
     * @param nowTime
     * @param startTime
     * @param endTime
     * @return 在时间段内返回true，不在返回false
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        return date.after(begin) && date.before(end);
    }


    /**
     * 修改课程购买
     *
     * @param kCoursePurchase 课程购买
     * @return 结果
     */
    @Override
    public int updateKCoursePurchase(KCoursePurchase kCoursePurchase) {
        kCoursePurchase.setUpdateTime(DateUtils.getNowDate());
        return kCoursePurchaseMapper.updateKCoursePurchase(kCoursePurchase);
    }

    /**
     * 批量删除课程购买
     *
     * @param ids 需要删除的课程购买主键
     * @return 结果
     */
    @Override
    public int deleteKCoursePurchaseByIds(String[] ids) {
        return kCoursePurchaseMapper.deleteKCoursePurchaseByIds(ids);
    }

    /**
     * 删除课程购买信息
     *
     * @param id 课程购买主键
     * @return 结果
     */
    @Override
    public int deleteKCoursePurchaseById(String id) {
        return kCoursePurchaseMapper.deleteKCoursePurchaseById(id);
    }
}
